require(['loading'], function(loading) {
	var addrId = window.location.search.substr(4); //获取url中的id参数
	var name,mobile,areaId,detail,zipCode,expressArea = '';
	//应用实例化
	var addAddress = new Vue({
		el: '#addAddress',
		data: {
			addressData: [],
			provinceData:[],
			cityData: [],
			countyData: [],
			showLoading: false,
			isProv: false,
			isCity: false,
			isCout: false,
			fastClick: false,
			areaUrl: wxUrl + '/v3.2/addr/areaList',
			addrUrl: wxUrl + '/v3.2/addr/getAddr',
			saveUrl: wxUrl + '/v3.2/addr/saveAddr',
			areaItem: {
				params:{
					token: token,
					userToken: userToken,
					parentAreaId: ''
				}
			},
			addrItem: {
				params:{
					token: token,
					userToken: userToken,
					addrId: addrId
				}
			},
			saveItem: {
				params:{
					token: token,
					userToken: userToken,
					addrId: addrId,
					areaId: areaId,
					detail: detail,
					mobile: mobile,
					name: name,
					zipCode: zipCode
				}
			}
		},
		ready: function() {
			if(addrId != null && addrId != ''){
				document.title = '编辑地址';
	        	this.getAddrData();
			}
	    },
		methods: {
			getAddrData: function() {
				//获取地址详细
				this.$http.get(this.addrUrl, this.addrItem)
					.then(function(response) {
						var result = JSON.parse(response.body);
						this.$set('addressData', result.data);
						var addressData = result.data;
						$('#people').val(addressData.name);
						$('#mobile').val(addressData.mobile);
						$('#address').val(addressData.province.name + " > " + addressData.city.name + " > " + addressData.county.name);
						$('#pcdId').val(addressData.county.id);
						$('#area').val(addressData.addrDetail);
						$('#isZipCode').val(addressData.zipCode);
					},function(response) {
						//errorCallback
					});
			},
			getProvince: function(){
				this.areaItem.params.parentAreaId = '';
				this.isProv = true;
				this.isCity = false;
				this.isCout = false;
				$("#areaBox").scrollTop(0);
				expressArea = "";
				//获取省份
				this.$http.get(this.areaUrl, this.areaItem)
					.then(function(response) {
						var result = JSON.parse(response.body);
						this.$set('provinceData', result.data.result);
					},function(response) {
						//errorCallback
					});
			},
			getCity: function(e){
				this.areaItem.params.parentAreaId = $(e.currentTarget).attr('id');
				$('.backCity').attr('id',$(e.currentTarget).attr('id'));
				this.isProv = false;
				this.isCity = true;
				this.isCout = false;
				expressArea += $(e.currentTarget).text() + " > ";
				$("#areaBox").scrollTop(0);
				//获取城市
				this.$http.get(this.areaUrl, this.areaItem)
					.then(function(response) {
						var result = JSON.parse(response.body);
						this.$set('cityData', result.data.result);
					},function(response) {
						//errorCallback
					});
			},
			getCounty: function(e){
				this.areaItem.params.parentAreaId = $(e.currentTarget).attr('id');
				this.isProv = false;
				this.isCity = false;
				this.isCout = true;
				expressArea += $(e.currentTarget).text() + " > ";
				$("#areaBox").scrollTop(0);
				//获取县区
				this.$http.get(this.areaUrl, this.areaItem)
					.then(function(response) {
						var result = JSON.parse(response.body);
						this.$set('countyData', result.data.result);
					},function(response) {
						//errorCallback
					});
			},
			overArea: function(e){
				this.isProv = false;
				this.isCity = false;
				this.isCout = false;
				$("#areaMask").fadeOut();
				$("#areaLayer").animate({"bottom": "-100%"});
				expressArea += $(e.currentTarget).text();
				$("#address").val(expressArea); //三级文字地址
				var dId = $(e.currentTarget).attr('id');
				$("#pcdId").val(dId); //获取区县的id
			},
			saveAddrUrl: function() {
				this.fastClick = true;
				this.saveItem.params.areaId = $("#pcdId").val();
				this.saveItem.params.name = $("#people").val();
				this.saveItem.params.mobile = $("#mobile").val();
				this.saveItem.params.detail = $("#area").val();
				this.saveItem.params.zipCode = $("#isZipCode").val();
				//保存新增地址
				this.$http.get(this.saveUrl, this.saveItem)
					.then(function(response) {
						var result = JSON.parse(response.body);
						var newAddrId = result.data.addrId;
						if(newAddrId != null && newAddrId != ''){
							if(addrId != null && addrId != ''){
								promptBox('地址编辑成功！');	
								setTimeout(function(){
									window.location.href="addressManagement.html";
								},1000);
							}else{
								promptBox('新地址添加成功！');
								setTimeout(function(){
									window.location.href="addressManagement.html";
								},1000);
							}
						}
					},function(response) {
						//errorCallback
					});
			}
		}
	});
	//表单验证
	$(function(){
		jQuery.validator.addMethod("phone", function(value, element) {
			var mobile = $("#mobile").val(); // 手机号码
			var mobileRule = /^1[3|4|5|7|8]\d{9}$/;
			if(isEmpty(mobile)) {
				return false;
			}else{
				if(!mobileRule.test(mobile)) {
					return false;
				}
			}
			return true;
		}, "请输入正确的手机号");
		jQuery.validator.addMethod("isZipCode", function(value, element) {
			var isZipCode = $("#isZipCode").val();//邮政编码
			var rule = /^[0-9]{6}$/;
			if(isEmpty(isZipCode)) {
				return true;
			}else{
				if(!rule.test(isZipCode)) {
					return false;
				}
			}
			return true;
		}, "请填写正确的邮政编码");
		function isEmpty(v, allowBlank) {
			return v === null || v === undefined || (!allowBlank ? v === "" : false);
		}
		jQuery("#address_form").validate({
			submitHandler:function(form){
				if(!addAddress.fastClick){
					addAddress.saveAddrUrl();//提交保存
				}
			},
			ignore: "",
			rules: {
				people: {
					required: true
				},
				area: {
					required: true,
				},
				pcdId: {
					required: true,
				},
				isZipCode: {
		            isZipCode: true
		        },
				mobile: {
					phone: []
				}
			},
			messages: {
				people: {
					required: "请输入联系人"
				},
				area: {
					required: "请输入详细地址"
				},
				pcdId: {
					required: "请输入地区信息",
				}
			}
		});
	});
	
	$(function(){
		//打开省市区选项
		$("#expressArea").click(function(){
			$("#areaMask").fadeIn();
			$("#areaLayer").animate({"bottom": 0});
		});
		//关闭省市区选项
		$("#areaMask, #closeArea").click(function(){
			$("#areaMask").fadeOut();
			$("#areaLayer").animate({"bottom": "-100%"});
		});
		var oHeight = $(document).height(); //浏览器当前的高度
	    var formHeight = $(".from_group").height();
	    var submitHeight = $(".submit").height();
	    $(".submit").css("margin-top",oHeight-formHeight-submitHeight)
	    $(window).resize(function(){   
	    	$(".submit").css("margin-top",oHeight-formHeight-submitHeight);
	    });
	});
});

// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory(false);
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
	goBackVHistory();
});
